<?php

namespace app\api\controller\v1\user;

use app\Request;
use app\services\pay\PayNotifyServices;
use app\services\user\UserRechargeServices;
use think\facade\Db;

/**
 * 充值类
 * Class UserRechargeController
 * @package app\api\controller\user
 */
class UserRechargeController
{
    protected $services = NUll;

    /**
     * UserRechargeController constructor.
     * @param UserRechargeServices $services
     */
    public function __construct(UserRechargeServices $services)
    {
        $this->services = $services;
    }

    public function wechatRecharge(Request $request)
    {
        [$price] = $request->postMore([
            ['price', 0],
        ], true);
        $storeMinRecharge = sys_config('store_user_min_recharge');
        if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
        $uid = (int)$request->uid();
        $recharge = $this->services->wechatRecharge($uid, $price);
        if ($recharge) {
            return app('json')->successful('', $recharge);
        }
        return app('json')->fail('充值失败');
    }

    //处理微信支付回调
    public function notify(PayNotifyServices $services)
    {
        $testxml = file_get_contents("php://input");
        putCallbackMsg($testxml);
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result  = json_decode($jsonxml, true);//转成数组，
        if ($result) {
            // 如果成功返回了
            $out_trade_no = $result['out_trade_no'];
            if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                $services->wechatUserRecharge($out_trade_no);
            }
        }
    }

    public function recharge(Request $request)
    {
        [$price, $recharId, $type, $from] = $request->postMore([
            ['price', 0],
            ['rechar_id', 0],
            ['type', 0],
            ['from', 'weixin']
        ], true);
        if (!$price || $price <= 0) return app('json')->fail('充值金额不能为0元!');
        if (!in_array($type, [0, 1])) return app('json')->fail('充值方式不支持!');
        if (!in_array($from, ['weixin', 'weixinh5', 'routine'])) return app('json')->fail('充值方式不支持');
        $storeMinRecharge = sys_config('store_user_min_recharge');
        if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
        $uid = (int)$request->uid();
        $re  = $this->services->recharge($uid, $price, $recharId, $type, $from);
        if ($re) {
            $msg = $re['msg'];
            unset($re['msg']);
            return app('json')->successful($msg, $re);
        }
        return app('json')->fail('充值失败');
    }

    /**
     * 小程序充值
     *
     * @param Request $request
     * @return mixed
     */
    public function routine(Request $request)
    {
        list($price, $recharId, $type) = $request->postMore([['price', 0], ['rechar_id', 0], ['type', 0]], true);
        if (!$price || $price <= 0) return app('json')->fail('充值金额不能为0元!');
        $storeMinRecharge = sys_config('store_user_min_recharge');
        if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
        $from = 'routine';
        $uid  = (int)$request->uid();
        $re   = $this->services->recharge($uid, $price, $recharId, $type, $from);
        if ($re) {
            $msg = $re['msg'];
            unset($re['msg']);
            return app('json')->successful($msg, $re);
        }
        return app('json')->fail('充值失败');
    }

    /**
     * 公众号充值
     * @param Request $request
     * @return mixed
     */
    public function wechat(Request $request)
    {
        list($price, $recharId, $from, $type) = $request->postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0]], true);
        if (!$price || $price <= 0) return app('json')->fail('充值金额不能为0元!');
        $storeMinRecharge = sys_config('store_user_min_recharge');
        if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
        $uid = (int)$request->uid();
        $re  = $this->services->recharge($uid, $price, $recharId, $type, $from);
        if ($re) {
            $msg = $re['msg'];
            unset($re['msg']);
            return app('json')->successful($msg, $re);
        }
        return app('json')->fail('充值失败');
    }

    /**
     * 充值额度选择
     * @return mixed
     */
    public function index()
    {
        //$rechargeQuota = sys_data('user_recharge_quota') ?? [];

        $arr           = Db::table('eb_system_config')->where('menu_name', 'like', 'recharge_amount_%')->field('id, value')->select();
        $rechargeQuota = array();
        foreach ($arr as $key => $value) {
            $rechargeQuota[$key]['id']         = $value['id'];
            $rechargeQuota[$key]['give_money'] = "0.00";
            $rechargeQuota[$key]['price']      = number_format($value['value'], 2);
        }

        $data['recharge_quota']     = $rechargeQuota;
        $recharge_attention         = sys_config('recharge_attention');
        $recharge_attention         = explode("\n", $recharge_attention);
        $data['recharge_attention'] = $recharge_attention;
        return app('json')->successful($data);
    }
}
